package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search")
public class SearchApiController {

    private final SearchService searchService;


    public SearchApiController(SearchService searchService) {
        this.searchService = searchService;
    }

    @Operation(summary = "仅用于测试-将指定已存在专辑上架保存到索引库")
    @Parameters({
            @Parameter(name = "albumId", description = "专辑ID", required = true)
    })
    @GetMapping("/albumInfo/upperAlbum/{albumId}")
    public Result<Void> upperAlbum(@PathVariable Long albumId) {
        searchService.upperAlbum(albumId);
        return Result.ok();
    }

    @Operation(summary = "该接口仅用于测试-下架专辑-删除文档")
    @Parameters({
            @Parameter(name = "albumId", description = "专辑ID", required = true)
    })
    @GetMapping("/albumInfo/lowerAlbum/{albumId}")
    public Result<Void> lowerAlbum(@PathVariable Long albumId){
        searchService.lowerAlbum(albumId);
        return Result.ok();
    }

    @Operation(summary = "站内检索，支持关键字、分类、标签条件分页检索，结果高亮")
    @Parameters({
            @Parameter(name = "albumIndexQuery", description = "专辑索引查询参数", required = true)
    })
    @PostMapping("/albumInfo")
    public Result<AlbumSearchResponseVo> search(@RequestBody AlbumIndexQuery albumIndexQuery) {
        AlbumSearchResponseVo searchResponseVo = searchService.search(albumIndexQuery);
        return Result.ok(searchResponseVo);
    }

    @Operation(summary = "查询指定一级分类下置顶3级分类下热门排行专辑")
    @Parameters({
            @Parameter(name = "category1Id", description = "一级分类ID", required = true)
    })
    @GetMapping("/albumInfo/channel/{category1Id}")
    public Result<List<Map<String, Object>>> getTopCategory3HotAlbumList(@PathVariable Long category1Id) {
        List<Map<String, Object>> topCategory3HotAlbums = this.searchService.getTopCategory3HotAlbums(category1Id);
        return Result.ok(topCategory3HotAlbums);
    }

    @Operation(summary = "用户输入关键字自动补全")
    @Parameters({
            @Parameter(name = "keyword", description = "用户输入关键字", required = true)
    })
    @GetMapping("/albumInfo/completeSuggest/{keyword}")
    public Result<List<String>> completeSuggest(@PathVariable String keyword) {
        List<String> suggestList = this.searchService.completeSuggest(keyword);
        return Result.ok(suggestList);
    }

    @Operation(summary = "更新所有分类下排行榜-手动调用")
    @GetMapping("/albumInfo/updateLatelyAlbumRanking")
    public Result<Void> updateLatelyAlbumRanking() {
        this.searchService.updateLatelyAlbumRanking();
        return Result.ok();
    }

    // /api/search/albumInfo/findRankingList/{category1Id}/{dimension}
    @Operation(summary = "获取指定一级分类下指定排序方式的排行榜")
    @Parameters({
            @Parameter(name = "category1Id", description = "一级分类ID", required = true),
            @Parameter(name = "dimension", description = "排序方式", required = true)
    })
    @GetMapping("/albumInfo/findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndex>> findRankingList(@PathVariable Long category1Id, @PathVariable String dimension) {
        List<AlbumInfoIndex> rankingList = this.searchService.getRankingList(category1Id, dimension);
        return Result.ok(rankingList);
    }

}

